package thomas.bier.client;

import thomas.bier.shared.Gebruiker;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RegistreerGUI {
	private BeerDelegate bDelegate = new BeerDelegate();

	public VerticalPanel registerShow() {
		VerticalPanel vp = new VerticalPanel();
		Grid g = new Grid(6, 2);

		Label headerLabel = new Label("Registratie");
		vp.add(headerLabel);

		//Labels
		g.setText(1,0,"Nickname:");
		g.setText(2,0,"E-mailadres:");		
		g.setText(3,0,"Geheime vraag:");
		g.setText(4,0,"Geheim antwoord:");

		//TextBox
		final TextBox nicknameTB = new TextBox();
		final TextBox emailTB = new TextBox();
		final PasswordTextBox wachtwoordTB = new PasswordTextBox();
		final TextBox geheime_vraagTB = new TextBox();
		final PasswordTextBox geheim_antwoordTB = new PasswordTextBox();


		//Verstuurknop
		Button versturen = new Button("Registreer");

		//Widgets
		g.setWidget(1,1,nicknameTB);
		g.setWidget(2,1,emailTB);		
		g.setWidget(3,1,geheime_vraagTB);
		g.setWidget(4,1,geheim_antwoordTB);
		g.setWidget(5,1,versturen);

		//Versturen
		versturen.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(
						!nicknameTB.getText().trim().isEmpty() &&
						emailTB.getText().matches("^[\\w\\.=-]+@[\\w\\.-]+\\.[\\w]{2,3}$") &&
						!wachtwoordTB.getText().trim().isEmpty() &&
						!geheime_vraagTB.getText().trim().isEmpty() &&
						!geheim_antwoordTB.getText().trim().isEmpty()
				) {
					bDelegate.registerUser(new Gebruiker(nicknameTB.getText(),emailTB.getText(),wachtwoordTB.getText(),geheime_vraagTB.getText(),geheim_antwoordTB.getText()));
				}
			}
		});
		vp.add(g);
		return vp;
	}

}
